'''
Created on 06/05/2012

@author: Tassio
'''

from PyQt4.QtCore import QAbstractTableModel, QVariant, Qt

class EntityModel(QAbstractTableModel):
    def __init__(self, parent=None):
        super(EntityModel, self).__init__(parent)
        self.entityList = [] 
        self.fields = []
        
    def setEntityList(self, entityList):
        self.entityList = entityList
        self.reset()
    def addField(self, attributeName, header):
        self.fields.append(_Field(attributeName, header))
       
    def rowCount(self, index):
        return len(self.entityList)
    def columnCount(self, index):
        return len(self.fields)
    def data(self, index, role): 
        if not index.isValid():
            return QVariant()
        
        if role == Qt.TextAlignmentRole:
            return Qt.AlignRight | Qt.AlignVCenter
        elif role == Qt.DisplayRole:
            entity = self.entityList[index.row()]
            return entity.__getattribute__(self.fields[index.column()].attributeName)
        
        return QVariant()  
    
    def headerData(self, section, orientation, role):
        if role != Qt.DisplayRole:
            return QVariant()
        
        if orientation == Qt.Horizontal:
            return self.fields[section].header
        else:
            return section+1
            
    
class _Field(object):
    def __init__(self, attributeName, header):
        self.attributeName = attributeName
        self.header = header
        #delegate
        #...

